package cn.metona.mq.config;

import java.util.Properties;
import java.util.logging.Logger;

/**
 * 配置管理器
 * 管理消息队列的各种配置参数
 */
public class ConfigManager {
    private static final Logger logger = Logger.getLogger(ConfigManager.class.getName());

    private static final ConfigManager INSTANCE = new ConfigManager();

    private final Properties properties;

    private ConfigManager() {
        this.properties = new Properties();
        loadDefaultConfig();
    }

    public static ConfigManager getInstance() {
        return INSTANCE;
    }

    /**
     * 加载默认配置
     */
    private void loadDefaultConfig() {
        // 消息队列配置
        properties.setProperty("mq.default.retry.count", "3");
        properties.setProperty("mq.default.batch.size", "10");
        properties.setProperty("mq.default.concurrency", "1");
        properties.setProperty("mq.default.queue.capacity", "10000");
        properties.setProperty("mq.default.message.ttl", "86400"); // 24小时

        // 监控配置
        properties.setProperty("mq.monitor.interval", "30");

        // 生产者配置
        properties.setProperty("mq.producer.max.pending", "1000");
        properties.setProperty("mq.producer.batch.size", "100");

        // 消费者配置
        properties.setProperty("mq.consumer.max.pending", "1000");
        properties.setProperty("mq.consumer.prefetch.count", "10");

        // 持久化配置
        properties.setProperty("mq.persistence.enabled", "true");
        properties.setProperty("mq.persistence.interval", "30");

        // 安全配置
        properties.setProperty("mq.security.enabled", "false");
        properties.setProperty("mq.security.auth.required", "false");

        logger.info("Default configuration loaded");
    }

    /**
     * 设置配置项
     */
    public void setProperty(String key, String value) {
        if (key == null || key.isEmpty()) {
            throw new IllegalArgumentException("Key cannot be null or empty");
        }
        if (value == null) {
            throw new IllegalArgumentException("Value cannot be null");
        }

        properties.setProperty(key, value);
        logger.info("Configuration updated: " + key + " = " + value);
    }

    /**
     * 获取字符串配置
     */
    public String getProperty(String key) {
        if (key == null || key.isEmpty()) {
            return null;
        }
        return properties.getProperty(key);
    }

    /**
     * 获取字符串配置（带默认值）
     */
    public String getProperty(String key, String defaultValue) {
        if (key == null || key.isEmpty()) {
            return defaultValue;
        }
        return properties.getProperty(key, defaultValue);
    }

    /**
     * 获取整数配置
     */
    public int getIntProperty(String key) {
        String value = getProperty(key);
        if (value == null) {
            throw new NumberFormatException("Property not found: " + key);
        }
        return Integer.parseInt(value);
    }

    /**
     * 获取整数配置（带默认值）
     */
    public int getIntProperty(String key, int defaultValue) {
        String value = getProperty(key);
        try {
            return value != null ? Integer.parseInt(value) : defaultValue;
        } catch (NumberFormatException e) {
            logger.warning("Invalid integer value for " + key + ": " + value + ", using default: " + defaultValue);
            return defaultValue;
        }
    }

    /**
     * 获取长整数配置
     */
    public long getLongProperty(String key) {
        String value = getProperty(key);
        if (value == null) {
            throw new NumberFormatException("Property not found: " + key);
        }
        return Long.parseLong(value);
    }

    /**
     * 获取长整数配置（带默认值）
     */
    public long getLongProperty(String key, long defaultValue) {
        String value = getProperty(key);
        try {
            return value != null ? Long.parseLong(value) : defaultValue;
        } catch (NumberFormatException e) {
            logger.warning("Invalid long value for " + key + ": " + value + ", using default: " + defaultValue);
            return defaultValue;
        }
    }

    /**
     * 获取布尔配置
     */
    public boolean getBooleanProperty(String key) {
        String value = getProperty(key);
        return Boolean.parseBoolean(value);
    }

    /**
     * 获取布尔配置（带默认值）
     */
    public boolean getBooleanProperty(String key, boolean defaultValue) {
        String value = getProperty(key);
        return value != null ? Boolean.parseBoolean(value) : defaultValue;
    }

    /**
     * 获取所有配置键
     */
    public String[] getPropertyNames() {
        return properties.stringPropertyNames().toArray(new String[0]);
    }

    /**
     * 重新加载配置
     */
    public void reload() {
        loadDefaultConfig();
        logger.info("Configuration reloaded");
    }
}